package com.example.student.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.student.pojo.Student;
import com.example.student.pojo.User;
import com.example.student.service.StudentService;
import com.example.student.service.UserService;
import com.sun.deploy.net.HttpResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@Slf4j
public class BaseController {
    @Autowired
    private UserService userService;
    @Autowired
    private StudentService studentService;


    /**
     * 前往登录页面
     * @return
     */
    @GetMapping("/")
    public String index(){
        return "login";
    }

    /**
     * 前往注册页面
     * @return
     */
    @GetMapping("/register")
    public String register(){
        return "register";
    }

    /**
     * 执行注册操作
     * @param user
     * @return
     */
    @PostMapping("/registerDo")
    public String registerDo(User user){
        String userName = user.getUserName();
        QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
        QueryWrapper<User> user_name = userQueryWrapper.eq("user_name", userName);
        User one = userService.getOne(userQueryWrapper);
        if (user.getUserName()!=null&&one==null){
            userService.save(user);
            return "login";
        }else {
            return "register";
        }

    }

    /**
     * 执行学生提交操作
     * @param student
     * @return
     */
    @PostMapping("/submitStu")
    public String submitStu(Student student){

        studentService.save(student);
        return "index";
    }

    /**
     * 执行登录操作
     * @param user
     * @return
     */
    @PostMapping("/login")
    public String login(User user){
        String userName = user.getUserName();
        QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
        userQueryWrapper.eq("user_name", userName);
        User one = userService.getOne(userQueryWrapper);
        if (one.getUserName().equals(user.getUserName()) && one.getPassword().equals(user.getPassword())){
            return "index";
        }else {
            return "login";
        }
    }
    /**
     * 渲染表单数据
     * @param id
     * @return
     */
    @GetMapping("/getJson/{id}")
    @ResponseBody
    public List students(@PathVariable String id){
        log.info(id);
        QueryWrapper<Student> studentQueryWrapper = new QueryWrapper<>();
        if (id!=null) studentQueryWrapper.like("id", id);
        List<Student> list ;
        return id.equals(0) ? studentService.list(): studentService.list(studentQueryWrapper);

    }

    /**
     * 执行删除操作
     * @param id
     * @return
     */
    @PostMapping("/del")
    @ResponseBody
    public String del(Integer id){
        log.info("执行删除操作");
        log.info(""+id);
        studentService.removeById(id);
        return "Yes";
    }

    /**
     * 前往填写学生信息
     * @return
     */
    @GetMapping("/toSub")
    public String toSub(){
        return "sub";
    }

}

